package com.sky.utils;



import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;


import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;

import org.springframework.stereotype.Component;

import java.io.BufferedReader;
import java.io.IOException;

import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

import java.text.DecimalFormat;
import java.util.Map;

@Component
@Slf4j
    public class BaiduAddressUtil {

    /**
     * 百度地图申请的ak
     */
    private static String AK="Wn9Dx5PPYDPHH0jkGIHajE4G60QgAc6Q";

        /**
         * 调用百度地图地理编码服务接口，根据地址获取坐标（经度、纬度）
         *
         * @param address
         * @return
         */
        public static String getCoordinate(String address) {
            String httpUrl = "http://api.map.baidu.com/geocoding/v3/?address=" + address + "&output=json&ak=" + AK;
            String json = loadJSON(httpUrl);
            Map map = JSON.parseObject(json, Map.class);

            String status = map.get("status").toString();
            if (status.equals("0")) {
                //返回结果成功，能够正常解析地址信息
                Map result = (Map) map.get("result");
                Map location = (Map) result.get("location");
                String lng = location.get("lng").toString();
                String lat = location.get("lat").toString();

                DecimalFormat df = new DecimalFormat("#.######");
                String lngStr = df.format(Double.parseDouble(lng));
                String latStr = df.format(Double.parseDouble(lat));
                String r = latStr + "," + lngStr;
                return r;
            }
            return null;
        }

        /**
         * 调用百度地图驾车路线规划服务接口，根据寄件人地址和收件人地址坐标计算订单距离
         *
         * @param origin
         * @param destination
         * @return
         */
        public static Double getDistance(String origin, String destination) {
            String httpUrl = "http://api.map.baidu.com/directionlite/v1/riding?origin="
                    +origin+"&destination="
                    +destination+"&ak=" + AK;
            String json = loadJSON(httpUrl);


            Map map = JSON.parseObject(json, Map.class);
            if ("0".equals(map.getOrDefault("status", "500").toString())) {
                Map childMap = (Map) map.get("result");
                System.out.println(childMap);
                JSONArray jsonArray = (JSONArray) childMap.get("routes");
                JSONObject jsonObject = (JSONObject) jsonArray.get(0);
                double distance = Double.parseDouble(jsonObject.get("distance") == null ? "0" : jsonObject.get("distance").toString());
                return distance;
            }

            return null;
        }

        /**
         * 调用百度地图驾车路线规划服务接口，根据寄件人地址和收件人地址坐标计算订单时间
         *
         * @param origin
         * @param destination
         * @return
         */
        public static Integer getTime(String origin, String destination) {
            String httpUrl = "http://api.map.baidu.com/directionlite/v1/driving?origin="
                    + origin + "&destination="
                    + destination + "&ak=" + AK;

            String json = loadJSON(httpUrl);

            Map map = JSON.parseObject(json, Map.class);
            if ("0".equals(map.getOrDefault("status", "500").toString())) {
                Map childMap = (Map) map.get("result");
                JSONArray jsonArray = (JSONArray) childMap.get("routes");
                JSONObject jsonObject = (JSONObject) jsonArray.get(0);
                int time = Integer.parseInt(jsonObject.get("duration") == null ? "0" : jsonObject.get("duration").toString());
                return time;
            }

            return null;
        }

        /**
         * 调用服务接口，返回百度地图服务端的结果
         *
         * @param httpUrl
         * @return
         */
        public static String loadJSON(String httpUrl) {
            StringBuilder sb = new StringBuilder();
            try {
                URL url = new URL(httpUrl);
                URLConnection urlConnection = url.openConnection();
                System.out.println(urlConnection);
                BufferedReader in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(), "UTF-8"));
                String inputLine = null;
                while ((inputLine = in.readLine()) != null) {
                    sb.append(inputLine);
                }
                in.close();
            } catch (MalformedURLException e) {
            } catch (IOException e) {
            }

            return sb.toString();

        }
    }


